package com.taotao.st3.base.redis;

import lombok.Getter;
import lombok.Setter;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.context.annotation.Bean;


/**
 * <pre>
 * ## redisson-single
 * redisson.address=redis://127.0.0.1:9090
 * redisson.password=
 *
 * ## redisson-sentinel
 * redisson.masterName=mymaster
 * redisson.schema=redis://
 * redisson.sentinelAddresses=101.241.71.70:26377,101.241.71.70:26378,101.241.71.70:26379
 * redisson.password=
 * </pre>
 */
@Getter
@Setter
//@Configuration
//@ConfigurationProperties(prefix = "redisson")
public class RedissionConfiguration {
    private int timeout = 3000;
    private String schema = "redis://";
    private String address;
    private String password;
    private int connectionPoolSize = 64;
    private int connectionMinimumIdleSize = 10;
    private int slaveConnectionPoolSize = 250;
    private int masterConnectionPoolSize = 250;
    private String[] sentinelAddresses;
    private String masterName;



    /*测试时mock使用*/
    /*@Bean(destroyMethod = "shutdown")
    RedissonClient redissonSingle() {
        RedissonClient redissonClient = Mockito.mock(RedissonClient.class);
        return redissonClient;
    }*/


    @Bean(destroyMethod = "shutdown")
    RedissonClient redissonClient() {
        if (null != sentinelAddresses) {
            return redissonSentinel();
        } else {
            return redissonSingle();
        }
    }

    /**
     * 单机模式
     */

    RedissonClient redissonSingle() {
        Config config = new Config();
        SingleServerConfig serverConfig = config.useSingleServer()
                .setAddress(address)
                .setTimeout(timeout)
                .setConnectionPoolSize(connectionPoolSize)
                .setConnectionMinimumIdleSize(getConnectionMinimumIdleSize());
        if (isNotBlank(password)) {
            serverConfig.setPassword(password);
        }
        return Redisson.create(config);
    }

    /**
     * 哨兵模式
     *
     * @return
     */
    RedissonClient redissonSentinel() {
        Config config = new Config();

        // 拼接协议
        String[] sentinelAddressesWithSchema = new String[sentinelAddresses.length];
        for (int i = 0; i < sentinelAddresses.length; i++) {
            sentinelAddressesWithSchema[i] = schema + sentinelAddresses[i];
        }

        SentinelServersConfig serversConfig = config.useSentinelServers()
                .setMasterName(masterName)
                .addSentinelAddress(sentinelAddressesWithSchema)
                .setMasterConnectionPoolSize(masterConnectionPoolSize)
                .setSlaveConnectionPoolSize(slaveConnectionPoolSize)
                .setMasterConnectionMinimumIdleSize(getConnectionMinimumIdleSize())
                .setSlaveConnectionMinimumIdleSize(getConnectionMinimumIdleSize());

        if (isNotBlank(password)) {
            serversConfig.setPassword(password);
        }
        return Redisson.create(config);
    }


    private boolean isNotBlank(String s) {
        return null != s && s.trim().length() > 0;
    }
}
